<?php

/**
 * Framework_Component
 *
 * @author      Ian Szewczyk <roamzero@gmail.com>
 * @copyright   (c) 2007 Ian Szewczyk. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package     Framework
 * @filesource
 */


/**
 * Framework_Component
 *
 *
 * @package     Framework
 * @link        http://smarty.php.net
 */
abstract class Framework_Component
{

	/**
	 * $components
	 *
	 * @access public
	 * @var array $components Collection of object components that have been loaded
	 * @static
	 */
	static private $components = array();

	

	/**
	 * factory
	 *
	 * @access public
	 * @param string $component
	 * @return mixed Instance of Framework_Component on success, PEAR_Error on failure
	 * @static
	 */
	static public function factory($component,$params)
	{
		if (!array_key_exists($component, self::$components)) {	
			$file = 'Framework/Component/'.ucfirst($component).'.php';
			if (!include_once($file)) {
				return PEAR::raiseError('Could not load component file: '.$file);
			}
	
			$class = 'Framework_Component_'.ucfirst($component);
			if (!class_exists($class)) {
				return PEAR::raiseError('Could not find component class: '.$class);
			}
	
			try {
				$instance = new $class($params);
				if (!$instance instanceof Framework_Component_Common) {
					return PEAR::raiseError('Component class must extend from Framework_Component_Common');
				}
			} catch (Framework_Exception $error) {
				return PEAR::raiseError($error->getMessage());
			}
			self::$components[$component] = $instance;
			return $instance;
		} else {
			return self::$components[$component];
		}
	}
}

?>
